import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:interview/components/interview/interview_list.dart';
import 'package:interview/components/status_bar_warpper.dart';
import 'package:interview/controller/interview_tabs_controller.dart';
import 'package:interview/controller/navigation_controller.dart';
import 'package:interview/theme/app_colors.dart';
import 'package:interview/views/interview/components/interview_header.dart';
import 'package:interview/views/interview/components/interview_keywords.dart';
import 'package:interview/views/interview/components/interview_tabbar.dart';

class Interview extends StatefulWidget {
  const Interview({super.key});

  @override
  State<Interview> createState() => _InterviewState();
}

class _InterviewState extends State<Interview> with TickerProviderStateMixin   {
  final InterviewTabsController interviewTabsController = Get.find<InterviewTabsController>();
  final navigation = Get.find<NavigationController>();
  late TabController _tabController;
  int currentIndex = 0; // 当前分类

  @override
  void initState() {
    super.initState();
    
    // 初始化 TabController，绑定到 tabs 的长度
    _tabController = TabController(
      length: interviewTabsController.tabs.length,
      vsync: this,
    );
    
    // 监听 tabs 的变化，动态更新 TabController
    ever(interviewTabsController.tabs, (_) {
      _updateTabController();
    });
  }

  // 更新 TabController 方法
  void _updateTabController() {
    if (!mounted) return;
    int newLength = interviewTabsController.tabs.length;
    if (_tabController.length == newLength) return;
    setState(() {
      _tabController.dispose();
      _tabController = TabController(
        length: newLength,
        vsync: this,
        initialIndex: currentIndex < newLength ? currentIndex : 0, // 确保索引有效
      );
    });
  }


  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  // Tab 切换逻辑
  void onTabTapped(int index) {
    setState(() {
      currentIndex = index;
      if (_tabController.index != index) {
        _tabController.animateTo(index);
      }
    });
  }

  void _tapKeyWords(String tab){
    navigation.push("/searchpage",parameters: {"tabKeyword":tab} );
  }

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context).extension<AppColors>()!;

    return StatusBarWrapper(
      statusBarColor: theme.subjectPure!,
      statusBarIconBrightness: Theme.of(context).brightness == Brightness.dark ? Brightness.light : Brightness.dark,
      child: SafeArea(
        child: Column(
          children: [
            // 顶部搜索框
            InterviewHeader(theme: theme),
            // 推荐区域
            InterviewKeywords(theme: theme , onTap: _tapKeyWords,),
            // 列表区域
            Expanded(
              child: Container(
                child: Column(
                  children: [
                    // 使用 Obx 包裹 TabBar
                    Obx(() {
                      interviewTabsController.tabs.toList(); // 确保引用到 RxList 一定访问 .toList()
                      return InterviewTabbar(
                        theme: theme,
                        currentIndex: currentIndex,
                        tabController: _tabController,
                        onTabTapped: onTabTapped,
                      );
                    }),
                    // TabBarView 也需要使用 Obx 包裹，确保动态更新
                    Expanded(
                      child: Obx(() {
                        final tabs = interviewTabsController.tabs.toList(); // 确保引用到 RxList
                        return TabBarView(
                          controller: _tabController,
                          children: tabs.map((tab) {
                            return InterviewList(tab: tab);
                          }).toList(),
                        );
                      }),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

